﻿/***
	user:XiaoJia
	qq:273142650
	email:iatt@qq.com
	date:2011.3.1
***/
(function() {
	document.write('<style>ul,li{margin:0;padding:0;list-style-type:none;text-decoration:none;}.Jcalendar{color:#000;font-size:12px;}.Jcalendar ul li{float:left;width:20px;height:20px;line-height:20px;text-align:center;font-weight:bold;color:#000;}.Jcalendar div{width:auto;height:20px;text-align:center;}.Jcalendar a{color:#000;text-decoration:none;}.Jcalendar a:hover{color:#ff0000;text-decoration:underline;}</style>');
	//style
	var days = new Array('31','28','31','30','31','30','31','31','30','31','30','31');
	var weekStyle = new Array('日','一','二','三','四','五','六');
	var myDate = new Date();
	var year = myDate.getFullYear();
	var months = myDate.getMonth() + 1;
	var day = myDate.getDate();
	var nYear = myDate.getFullYear();
	var nMonths = myDate.getMonth() + 1;
	var nDay = myDate.getDate();
	var triangle = 'border-style:solid;border-width:5px 15px 5px 15px;width:0;height:0;'
	//css triangle
	var divCss = ';width:140px;height:auto;top:0;background:#fff;left:0;border:1px #000 solid;position:absolute;';
	//div style
	var userCss = null;
	//div background
	var id = 'JcalendarDiv';
	//div id
	var yyyy = 'yyyy-mm-dd';
	//date style
	var Obj = null;
	//id or object
	var Jq = false;
	( typeof(window.jQuery) == "undefined" ) ? Jq = false : Jq = true;
	window.Jcalendar=function(obj){
		if( obj != null ){
			typeof( obj ) == 'string' ? Obj = document.getElementById( obj ) : Obj = obj;
		}
		return new newCalendar();
	}
	var newCalendar = function(){}
	newCalendar.prototype = {
		css: function( css ){
			userCss = css;
			return this;
		},
		id: function( Id ){
			Id != '' ? id = Id : '';
			return this;
		},
		yyyy: function( yy ){
			yy != '' ? yyyy = yy : '';
			return this;
		},
		newDate: function( date ){
			if( date.search(/\d{4}\D\d{1,2}\D\d{1,2}/ig,'')==0 ){
				date = date.split(/\D/ig);
				year = date[0];
				months = date[1];
				day = date[2];
				nYear = year;
				nMonths = months;
				nDay = day;
			}else{
				myDate = new Date();
				year = myDate.getFullYear();
				months = myDate.getMonth() + 1;
				day = myDate.getDate();
				nYear = myDate.getFullYear();
				nMonths = myDate.getMonth() + 1;
				nDay = myDate.getDate();
			}
			return this;
		},
		jq: function( jQ ){
			jQ != null ? Jq = jQ : '';
			return this;
		},
		calendar: function( val ){
			if( val != null ){
				L = typeof( val.L ) == 'undefined' ? null : val.L;
				T = typeof( val.T ) == 'undefined' ? null : val.T;
			}else{
				L = null;
				T = null;
			}
			obj = Obj;
			divTop = this.offset( obj ).split(',')[0];
			divLeft = this.offset( obj ).split(',')[1];
			div = document.createElement('div');
			div.className = 'Jcalendar';
			div.id = id;
			div.style.cssText = divCss;
			if( L != null ){
				L.toString().search(/^\D/ig) == 0 ? divLeft = parseInt( divLeft ) + ( parseInt(L) ) : divLeft = L;
			}
			if( T != null ){
				T.toString().search(/^\D/ig) == 0 ? divTop = parseInt( divTop ) + ( parseInt( T ) ) : divTop = T;
			}
			div.style.top = divTop + 'px';
			div.style.left = divLeft + 'px';
			if( userCss != null ){
				for( var i in userCss ){
					eval( 'div.style.' + i + '="' + userCss[i] + '"' );
				}
			}
			document.getElementById( id ) != null ? document.body.removeChild( document.getElementById( id ) ) : '';
			document.body.appendChild( div );
			this.format();
			if( Jq ){
				$('#'+id).fadeOut(0,function(){$('#'+id).fadeIn(200)});
			}
			return this;
		},
		format: function( v ){
			if( v != undefined ){
				year = v.y;
				months = v.m;
			}
			var wEEK = '';
			for( i = 0; i < weekStyle.length; i++){
				wEEK = wEEK + '<li>' + weekStyle[i] + '</li>';
			}
			days[1] = year % 4 == 0 ? days[1] = 29 : days[1] = 28;
			document.getElementById( id ).innerHTML = '<div><div style="left:5px;top:0;position:absolute;"><a href="###" onclick="Jcalendar(document.body).calendarClose(\''+id+'\');">X</a></div><a href="###" onclick="Jcalendar().upMonths()">上</a>&nbsp;<a href="###" onclick="Jcalendar().year(this);">'+year+'</a>-<a href="###" onclick="Jcalendar().months(this)">'+this.add0(months)+'</a>&nbsp;<a href="###" onclick="Jcalendar().downMonths()">下</a></div><ul>'+wEEK+this.day()+'</ul>';
			if( Jq ){
				$('#'+id).find('ul:eq(0)').fadeOut(0,function(){$('#'+id).find('ul:eq(0)').fadeIn(250);});
			}
		},
		day: function(){
			var d = '';
			var l = '';
			week = new Date( Date.parse( year + '/' + months + '/' + '1' ) );
			for( i = 0 ; i < week.getDay(); i++ ){
				l = l + '<li></li>';
			}
			for( i = 1 ; i <= days[months-1] ; i++ ){
				if( i == day && year==nYear && months == nMonths ){
					d = d + '<li><a href="###" onclick="Jcalendar().onMouseDown(this.innerHTML)" style="color:#ff0000;">' + this.add0(i) + '</a></li>';
				}else{
					d = d + '<li><a href="###" onclick="Jcalendar().onMouseDown(this.innerHTML)">' + this.add0(i) + '</a></li>';
				}
			}
			return l + d;
		},
		add0: function( num ){
			if( parseInt( num ) < 10 && num.toString().search(/^0/ig) != 0 ){
				num = '0' + num
			}
			return num;
		},
		upMonths: function(){
			months--;
			if( months <= 0 ){
				months = 12;
				year--;
			}
			this.format();
		},
		downMonths: function(){
			months++;
			if( months > 12 ){
				months = 1;
				year++;
			}
			this.format();
		},
		upYear: function( ty ){
			document.getElementById( id + 'year' ).innerHTML = '<ul>' + Jcalendar().yearList( parseInt( parseInt( ty ) - 1 ) ) + '</ul>';
			s = function(){Jcalendar().upYear( ty - 1 )}
			t = setTimeout(s,100);
			document.onmouseup = function(){
				clearTimeout( t );
			}
		},
		downYear: function( ty ){
			document.getElementById( id + 'year' ).innerHTML = '<ul>' + Jcalendar().yearList( parseInt( parseInt( ty ) + 1 ) )+'</ul>';
			s = function(){Jcalendar().downYear( parseInt( ty ) + 1 )}
			t = setTimeout(s,100);
			document.onmouseup = function(){
				clearTimeout( t );
			}
		},
		onMouseDown: function( dDay ){
			ymd = yyyy.replace(/y{4}/ig,year).replace(/m{2}/ig,this.add0( months )).replace(/d{2}/ig,dDay).replace(/w/ig,weekStyle[new Date( Date.parse(year+'/'+months+'/'+dDay) ).getDay()]).replace(/y{2}/ig,year.toString().substring(2,4));
			( typeof( Obj.type ) != 'undefined' ) ? Obj.value = ymd : Obj.innerHTML = ymd;
			Jcalendar( document.body ).calendarClose( id );
		},
		year: function( obj ){
			yid = id + 'year';
			div = document.createElement('div');
			div.style.cssText = ';width:40px;height:auto;background:#fff;border:solid 1px #0099CC;position:absolute;top:17px;left:40px;';
			div.id = yid;
			div.innerHTML = '<ul>' + this.yearList( obj.innerHTML ) + '</ul>';
			document.getElementById( yid ) != null ? document.getElementById( id ).removeChild( document.getElementById( yid ) ) : '';
			document.getElementById( id ).appendChild( div );
			if( Jq ){
				$('#'+yid).fadeOut(0,function(){$('#'+yid).fadeIn(200)});
			}
		},
		months: function( obj ){
			mid = id + 'months';
			div = document.createElement('div');
			div.style.cssText = ';width:20px;height:auto;background:#fff;border:solid 1px #0099cc;position:absolute;top:17px;left:75px;';
			div.id = mid;
			div.innerHTML = '<ul>' + this.monthsList( obj.innerHTML ) + '</ul>';
			document.getElementById( mid ) != null ? document.getElementById( id ).removeChild( document.getElementById( mid ) ) : '';
			document.getElementById( id ).appendChild( div );
			if( Jq ){
				$('#'+mid).fadeOut(0,function(){$('#'+mid).fadeIn(200)});
			}
		},
		yearList: function( ty ){
			var yl = '';
			ty1 = parseInt( ty ) - 5 < 1000 ? 1000 : parseInt( ty ) - 5;
			ty2 = parseInt( ty ) + 5 > 9999 ? 9999 : parseInt( ty ) + 5;
			for( i = ty1 ; i <= ty2 ; i++ ){
				if( i == year ){
					yl = yl + '<li style="width:40px;"><a href="###" style="color:#ff0000;" onclick="Jcalendar().format({y:this.innerHTML,m:'+months+'})">' + i + '</a></li>';
				}else{
					yl = yl + '<li style="width:40px"><a href="###" onclick="Jcalendar().format({y:this.innerHTML,m:'+months+'})">' + i + '</a></li>';
				}
			}
			return '<li style="width:40px;"><a href="###" onmousedown="Jcalendar().downYear(\''+ty+'\')">＋</a><a href="###" onmousedown="Jcalendar().upYear(\''+ty+'\')">－</a></li>' + yl;
		},
		monthsList: function( ty ){
			var yl = '';
			for( i = 1 ; i <= 12 ; i++ ){
				if( i == months ){
					yl = yl + '<li style="width:20px;"><a href="###" style="color:#ff0000;" onclick="Jcalendar().format({y:'+year+',m:this.innerHTML})">' + this.add0(i) + '</a></li>';
				}else{
					yl = yl + '<li style="width:20px"><a href="###" onclick="Jcalendar().format({y:'+year+',m:this.innerHTML})">' + this.add0(i) + '</a></li>';
				}
			}
			return yl;			
		},
		offset: function( obj ){
			divTop = obj.offsetTop;
			divLeft = obj.offsetLeft;
			while( obj = obj.offsetParent ){
				divTop += obj.offsetTop;
				divLeft += obj.offsetLeft;
			}
			return divTop + ',' + divLeft;
		},
		calendarClose: function( cid ){
			if( Jq ){
				$('#'+id).stop();
				$('#'+id).fadeOut(200,function(){Obj.removeChild(document.getElementById(cid));});
			}else{
				Obj.removeChild( document.getElementById( cid ) );
			}
		}
	}
})();